﻿using Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business
{
    public class UpdateVersionBll
    {
        private static readonly string UP_APPPATH = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"DownAppFile\";
        private static readonly string UploadWebAppHost = ConfigurationManager.AppSettings["UpdateAppVeersionHost"].ToString();

        public string CheckAppVersion(int majorId, int buildNo)
        {
            int versionNO = majorId * 10 + buildNo;
            int _MaxVersion = 0;
            string[] Directorys = System.IO.Directory.GetDirectories(UP_APPPATH);
            if (Directorys.Length > 0)
            {
                foreach (var _directory in Directorys)
                {
                    string _directoryName = new DirectoryInfo(_directory).Name;
                    string[] split = _directoryName.Split('.');
                    string versionCode = split[0];
                    int _majorID = Convert.ToInt32(split[1]);
                    int _buidNo = Convert.ToInt32(split[2]);
                    int _version = _majorID * 10 + _buidNo;
                    if (_version > _MaxVersion) _MaxVersion = _version;
                }
            }
            if (_MaxVersion > versionNO)
            {
                VersionInfo _resulf = new VersionInfo();
                int versionNo = majorId * 10 + buildNo;
                int _maxVersion = 0;
                //获取最大版本号的文件夹
                string[] directorys = System.IO.Directory.GetDirectories(UP_APPPATH);
                if (directorys.Length > 0) //如果该目录总没有其他文件夹
                {
                    foreach (var _directory in directorys)
                    {
                        string _directoryName = new DirectoryInfo(_directory).Name;
                        string[] split = _directoryName.Split('.');
                        string versionCode = split[0];
                        int _majorID = Convert.ToInt32(split[1]);
                        int _buidNo = Convert.ToInt32(split[2]);
                        int _version = _majorID * 10 + _buidNo;
                        if (_version > versionNo && _version > _maxVersion)
                        {
                            _maxVersion = _version;

                            _resulf.MAJORID = _majorID;
                            _resulf.BIULDNO = _buidNo;
                            _resulf.VERSIONDESC = versionCode;
                            _resulf.RELEASENOTE = _directoryName;
                        }
                    }
                }
                //设置最大版本号的数据
                string _result = "";
                string _FilePath = UP_APPPATH + _resulf.RELEASENOTE;
                DirectoryInfo folder = new DirectoryInfo(_FilePath);
                List<DownloadFileInfo> _lstDownFile = new List<DownloadFileInfo>();
                if (folder.Exists)
                {
                    foreach (FileInfo _file in folder.GetFiles("*.*"))
                    {
                        if (_file.Exists && !string.IsNullOrEmpty(_file.Extension))
                        {
                            string FILENAME = System.IO.Path.GetFileName(_file.FullName);
                            string EXTENSION = _file.Extension;

                            _result = UploadWebAppHost + "" + "DownAppFile/" + _resulf.RELEASENOTE + "/" + FILENAME;
                        }
                    }
                }
                return _result;
            }
            else
            {
                return "";
            }
        }

        public VersionInfo GetLastAppVersion(int majorId, int buildNo)
        {
            try
            {
                VersionInfo _resulf = new VersionInfo();
                int versionNo = majorId * 10 + buildNo;
                int _maxVersion = 0;
                //获取最大版本号的文件夹
                string[] directorys = System.IO.Directory.GetDirectories(UP_APPPATH);
                if (directorys.Length > 0) //如果该目录总没有其他文件夹
                {
                    foreach (var _directory in directorys)
                    {
                        string _directoryName = new DirectoryInfo(_directory).Name;
                        string[] split = _directoryName.Split('.');
                        string versionCode = split[0];
                        int _majorID = Convert.ToInt32(split[1]);
                        int _buidNo = Convert.ToInt32(split[2]);
                        int _version = _majorID * 10 + _buidNo;
                        if (_version > versionNo && _version > _maxVersion)
                        {
                            _maxVersion = _version;

                            _resulf.MAJORID = _majorID;
                            _resulf.BIULDNO = _buidNo;
                            _resulf.VERSIONDESC = versionCode;
                            _resulf.RELEASENOTE = _directoryName;
                        }
                    }
                }

                //设置最大版本号的数据
                string _FilePath = UP_APPPATH + _resulf.RELEASENOTE;
                DirectoryInfo folder = new DirectoryInfo(_FilePath);
                List<DownloadFileInfo> _lstDownFile = new List<DownloadFileInfo>();
                if (folder.Exists)
                {
                    foreach (FileInfo _file in folder.GetFiles("*.*"))
                    {
                        if (_file.Exists && !string.IsNullOrEmpty(_file.Extension))
                        {
                            DownloadFileInfo _Downfile = new DownloadFileInfo();
                            _Downfile.FILENAME = System.IO.Path.GetFileName(_file.FullName);
                            _Downfile.EXTENSION = _file.Extension;
                            _lstDownFile.Add(_Downfile);
                        }
                    }
                }

                _resulf.DownFiles = _lstDownFile;
                return _resulf;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }
        }
    }
}
